<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form id="formOpe">
    <input type="hidden" name="userId"> <br>
    <input type="text" name="userName" placeholder="用户姓名">用户姓名
    <input type="button" id="checkName" value="查重"><br>
    <input type="text" name="password" placeholder="密码">密码 <br>
    <input type="text" name="repassword" placeholder="确认密码">确认密码 <br>
    <input type="text" name="py" placeholder="拼音码">拼音码 <br>
    <input type="text" name="salary" placeholder="工资">工资 <br>
    <input type="text" name="nativePlaceProvince" placeholder="籍贯省">籍贯省 <br>
    <input type="text" name="nativePlaceCity" placeholder="市">市 <br>
    <input type="date" name="dataOfBirth" placeholder="出生日期">出生日期 <br>
<!--    <input type="text" name="enabled" placeholder="可用标记">可用标记 <br>-->
    <input type="radio" id="eanable" name="enabled" value="0" checked="checked" >启用
    <input type="radio" id="disable" name="enabled" value="2" >停用 <br>
    <input type="button" id="btnOpe" value="保存">
    <input type="button" value="取消" onclick="javascript:history.back(-1);">
</form>

<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>

<script>
    $("#btnOpe").click(function () {
        var arr = $("#formOpe").serializeArray();
        var obj={}
        $(arr).each(function (index,item) {
            obj[item.name]=item.value
        })
        console.log(obj);

        var url;
        var type;
        if(sessionStorage.getItem("id")){
            url='/user/edit';
            type='put';
        }else{
            url='/user/add';
            type='post';
        }

        $.ajax({
            url:url,
            type:type,
            data: JSON.stringify(obj),
            contentType:'application/json', // 传到后端的类型
            dataType:'json',                // 后端返回的类型
            success:function (res) {
                if(res.success){
                    window.location.href="/page/list.html"
                }else{
                    alert(res.msg);
                }
            }
        })
    });
    $("#checkName").click(function () {
        let userName = $("input[name='userName']").val();
        let userId = $("input[name='userId']").val();
let data={"userName":userName,"userId":userId};
        $.ajax({
            url:'/user/checkName',
            type: 'get',
            data: data,
            contentType:'application/json', // 传到后端的类型
            dataType:'json',                // 后端返回的类型
            success:function (res) {
                if(res.success){

                    alert(res.msg);
                }else{
                    alert(res.msg);
                }
            }
        })
    });
    $(function () {
        var id = sessionStorage.getItem("id");
        if(id){
            // 回显
            $.ajax({
                url:'/user/'+id,
                type:'get',
                data: '',
                dataType:'json',                // 反端返回的类型
                success:function (res) {
                    if(res.success){
                        var user = res.data;
                        $("input[name='userId']").val(user.userId);
                        $("input[name='userName']").val(user.userName);
                        // $("input[name='password']").val(user.password);
                        // $("input[name='repassword']").val(user.password);
                        $("input[name='py']").val(user.py);
                        $("input[name='salary']").val(user.salary);
                        $("input[name='nativePlaceProvince']").val(user.nativePlaceProvince);
                        $("input[name='nativePlaceCity']").val(user.nativePlaceCity);
                        $("input[name='dataOfBirth']").val(user.dataOfBirth);
                        // $("input[name='enabled']").val(user.enabled);
                        if (user.enabled==="0"){
                            console.log("enable")
                            $("#enable").attr("checked","checked")
                            $("#disable").removeAttr("checked")
                        }else{
                            $("#enable").removeAttr("checked")
                            $("#disable").attr("checked","checked")

                        }
                    }else{
                        alert(res.msg);
                    }

                }
            })
        }
    })
</script>
</body>
</html>
